package org.example.framework.dto;

/**
 * @author tangxiao
 */
public class ResponseDTO<T> {

    private static final String SUCCESS_CODE = "0000";

    private T data;

    private String code;

    private String msg;

    private ResponseDTO(T data) {
        this.data = data;
        this.initDefault();
    }

    private ResponseDTO(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <D> ResponseDTO ok(D data) {
        return new ResponseDTO<>(data);
    }


    public static ResponseDTO fail(String code, String msg) {
        return new ResponseDTO<>(code, msg);
    }

    private void initDefault() {
        this.code = SUCCESS_CODE;
    }

    public T getData() {
        return data;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    @Override
    public String toString() {
        return String.format("ResponseDTO{data=%s, code=%s, msg=%s}", data, code, msg);
    }
}
